﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Commerce.Base.Ubl.DataType
{
    /// <summary>
    /// A counted number of non-monetary units possibly including fractions.
    /// </summary>
    public class Quantity
    {
        private System.Decimal value = System.Decimal.Zero;
        private Code unitCode;

        /// <summary>
        /// The quantity value.
        /// </summary>
        public System.Decimal Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        /// <summary>
        /// The unit of the quantity.
        /// </summary>
        public Code UnitCode
        {
            get { return unitCode; }
            set { unitCode = value; }
        }

        public Quantity(System.Decimal value)
        {
            this.value = value;
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.unitCode))
                return string.Format("{0} {1}", this.value, this.unitCode);
            else
                return this.value.ToString();
        }

        public static implicit operator Quantity(System.Decimal value)
        {
            return new Quantity(value);
        }

        public static implicit operator System.Decimal(Quantity quantity)
        {
            return quantity.value;
        }
    }
}
